package edu.wpi.cs509.ui;

import java.awt.Dimension;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;

import edu.wpi.cs509.ui.GameWindowFrame.SubmitHandler;
import edu.wpi.cs509.ui.GameWindowFrame.SurrenderHandler;

/**
 * 
 * @author afoltan
 *
 */
@SuppressWarnings("serial")
public class GameWindowControls extends JPanel {

	static final int BORDER = 8;
	
	private int width;
	private int height;
	
	private JButton submitButton;
	private JButton surrenderButton;
	
	private boolean observer;

	public GameWindowControls( SubmitHandler submitHandler, SurrenderHandler surrenderHandler ) {

		if ((submitHandler == null) && (surrenderHandler==null)) {
			observer = true;
		}
		
		setLayout( new BoxLayout(this, BoxLayout.X_AXIS) );
		
		submitButton = new JButton("Submit");
		surrenderButton = new JButton("Quit!");

		if (submitHandler != null) {
			submitButton.addActionListener( submitHandler );
			submitButton.setEnabled(false);
		}
		
		if (surrenderHandler != null) {
			surrenderButton.addActionListener( surrenderHandler );
		}
		
		add(submitButton);
		add(surrenderButton);

		width = BORDER + submitButton.getPreferredSize().width + BORDER + surrenderButton.getPreferredSize().width + BORDER;
		height = BORDER + submitButton.getPreferredSize().height + BORDER;
		
		Dimension d = new Dimension( width, height );
		setPreferredSize( d );
		setMinimumSize( d );
		
		if (observer) {
			submitButton.setEnabled(false);
			surrenderButton.setEnabled(false);
		}
		
	}

	/**
	 * Changes the text on the Quit button
	 * @param string
	 */
	public void displaySurrenderButton() {
		if (surrenderButton != null) {
			surrenderButton.setText("Surrender!");
		}
	}
    /** Changes the text on the Surrender button back to Quit
     * 
     * 
     */
	public void displayQuitButton() {
		if (surrenderButton != null) {
			surrenderButton.setText("Quit");
		}
	}
	/**
	 * Changes the text on the Submit button
	 * @param string
	 */
	public void displayMoveButton() {
		if (submitButton != null) {
			submitButton.setText("Move");
		}
	}
	
	public void disableSubmitMoveButton() {
		if (observer) return;
		submitButton.setEnabled(false);
	}

	public void enableSubmitMoveButton() {
		if (observer) return;
		submitButton.setEnabled(true);
	}

}
